<template>
  <div>
    <el-card shadow="never" class="d2-mb">
      <el-form
        class="filter-container"
        ref="egForm"
        :model="form_first"
        label-width="100px"
      >
        <el-row :gutter="20">
          <el-col :span="4">
            <el-form-item label="指令名称">
              <el-input
                placeholder="请输入内容"
                v-model="form_first.command_name"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="指令ID">
              <el-input
                placeholder="请输入内容"
                v-model="form_first.command_id"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="指令类型">
              <el-select v-model="form_first.command_type" placeholder="请选择">
                <el-option
                  v-for="item in type_options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="所属策略名称">
              <el-input
                placeholder="请输入内容"
                v-model="form_first.strategy_name"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="所属策略ID">
              <el-input
                placeholder="请输入内容"
                v-model="form_first.strategy_id"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="部门名称">
              <el-input
                placeholder="请输入内容"
                v-model="form_first.department_name"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="申请人">
              <el-input
                placeholder="请输入内容"
                v-model="form_first.creator"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="状态">
              <el-select v-model="form_first.status" placeholder="请选择">
                <el-option
                  v-for="item in first_statusData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2" style="margin-top:4px;">
            <el-button type="primary" size="small" @click="selectDataone">
              <d2-icon name="search" /> 查询
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <div class="mb">
      <el-button type="default" size="mini" @click="handleFilter_addqhzl(1)">
        <d2-icon name="plus" />
        新增
      </el-button>
      <el-button type="default" size="mini" @click="handleFilter_editCkgl">
        <d2-icon name="edit" />
        修改
      </el-button>
      <el-button
        type="default"
        size="mini"
        icon="el-icon-delete"
        @click="delete_fn"
      >
        删除
      </el-button>
      <el-button
        type="default"
        size="mini"
        icon="el-icon-delete"
        @click="look_fn"
      >
        查看
      </el-button>
      <el-button type="default" size="mini" @click="handleFilter_addqhzl(2)">
        <i class="fa fa-clipboard" aria-hidden="true"></i>
        复制
      </el-button>
      <el-button
        type="default"
        size="mini"
        @click="submit_first_dialogVisible_fn"
        :loading="loading_first"
      >
        <i class="fa fa-calendar-check-o" aria-hidden="true"></i>
        提交审批
      </el-button>
      <el-button type="default" size="mini" @click="handleFilter_qhzlTjsp_tui">
        <i class="fa fa-calendar-times-o" aria-hidden="true"></i>
        退审
      </el-button>
      <el-button type="default" size="mini" @click="tiaozhuan()">
        <i class="fa fa-calendar-plus-o" aria-hidden="true"></i>
        审批历史
      </el-button>
      <el-button
        type="default"
        size="mini"
        @click="handleCleanCacheAndRefreshCurrent"
      >
        <d2-icon name="refresh" />
        刷新
      </el-button>
      <el-button type="default" size="mini" @click="order_fn_first">
        <i class="fa fa-calendar-times-o" aria-hidden="true"></i>
        下达指令
      </el-button>
      <el-button type="default" size="mini" @click="order_fn_first_null">
        <i class="fa fa-calendar-times-o" aria-hidden="true"></i>
        撤销指令
      </el-button>
    </div>
    <el-card shadow="never" class="d2-mb titleline">
      <div>
        <el-table
          :data="tableData_first"
          style="width: 100%"
          highlight-current-row
          border
          @current-change="handleCurrentChange_first"
          :cell-style="{ 'text-align': 'center' }"
          :header-cell-style="{
            background: '#eef1f6',
            'text-align': 'center',
            color: '#606266'
          }"
        >
          <el-table-column type="index" width="55"> </el-table-column>
          <el-table-column label="状态" prop="status"> </el-table-column>
          <el-table-column label="机构名称" prop="org_name"> </el-table-column>
          <el-table-column label="部门名称" prop="depart_name">
          </el-table-column>
          <el-table-column label="指令名称" prop="command_name">
          </el-table-column>
          <el-table-column label="指令ID" prop="command_id" sortable>
          </el-table-column>
          <el-table-column label="指令类型" prop="command_type">
          </el-table-column>
          <el-table-column label="所属策略名称" prop="strategy_name">
          </el-table-column>
          <el-table-column label="所属策略ID" prop="strategy_id" sortable>
          </el-table-column>
          <el-table-column label="指令开始时间" prop="start_date" sortable>
          </el-table-column>
          <el-table-column label="指令结束时间" prop="end_date" sortable>
          </el-table-column>
          <el-table-column label="申请人" prop="creator_name">
          </el-table-column>
        </el-table>
        <el-pagination
          @current-change="handlePaginationChange"
          :current-page="form_first.page"
          :page-size="form_first.pageSize"
          layout="total, prev, pager, next"
          :total="form_first.pageTotal"
        >
        </el-pagination>
      </div>
    </el-card>
    <el-dialog
      title="期权指令-查看"
      :visible.sync="dialogVisible_look"
      width="80%"
      :append-to-body="true"
    >
      <div class="mb">
        <el-button
          type="default"
          size="mini"
          @click="dialogVisible_look = false"
        >
          <d2-icon name="plus" />
          关闭
        </el-button>
        <el-button type="default" size="mini" @click="tiaozhuan()">
          <d2-icon name="plus" />
          审批历史
        </el-button>
      </div>
      <el-table
        class="d2-mb"
        :data="look_first_data_t"
        border
        fit
        :cell-style="{ 'text-align': 'center' }"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="strategy_name" label="策略名称">
        </el-table-column>
        <el-table-column prop="strategy_id" label="策略ID"> </el-table-column>
        <el-table-column prop="command_type" label="指令类型">
        </el-table-column>
        <el-table-column prop="command_name" label="指令名称">
        </el-table-column>
        <el-table-column prop="command_id" label="指令ID"> </el-table-column>
        <el-table-column label="指令开始时间" prop="start_date">
        </el-table-column>
        <el-table-column prop="end_date" label="指令结束时间">
        </el-table-column>
        <el-table-column prop="depart_name" label="部门名称"> </el-table-column>
        <el-table-column prop="real_account" label="柜台账号">
        </el-table-column>
        <el-table-column prop="sub_account" label="子账号"> </el-table-column>
        <el-table-column prop="trader" label="交易员"> </el-table-column>
      </el-table>
      <el-table
        :data="look_first_data_b"
        border
        fit
        :cell-style="{ 'text-align': 'center' }"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="productName" label="品名"> </el-table-column>
        <el-table-column prop="instrument" label="合约" width="240px">
        </el-table-column>
        <el-table-column prop="direction" label="方向" sortable>
        </el-table-column>
        <el-table-column prop="offset_flag" label="开/平" sortable>
        </el-table-column>
        <el-table-column prop="volume" label="手数" sortable> </el-table-column>
        <el-table-column label="价格区间" width="240px">
          <template slot-scope="scope">
            {{ scope.row.min_price }}-{{ scope.row.max_price }}
          </template>
        </el-table-column>
        <el-table-column prop="commodity_unit" label="单位" sortable>
        </el-table-column>
        <el-table-column prop="currency" label="币种" sortable>
        </el-table-column>
        <el-table-column prop="name.remarks" label="备注" sortable>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>
<script>
import {
  futures_command_info,
  getFutures_command_info,
  getCommand_self_info,
  getCommand_self_trade,
  getFutures_command_trade,
  command_self_info,
  work_order,
  getUsers,
  currency
} from "@/api/url";
export default {
  data() {
    return {
      form_first: {
        command_trade_type: 2,
        command_name: "",
        command_id: "",
        command_type: "",
        strategy_name: "",
        strategy_id: "",
        department_name: "",
        creator: "",
        status: "",
        page: 1,
        pageSize: 20,
        pageTotal: 0
      },
      dialogVisible_look: false,
      tableData_first: [],
      loading_first: false,
      look_first_data_t: [],
      look_first_data_b: [],
      type_options: [
        {
          value: 1,
          label: "普通指令"
        },
        {
          value: 2,
          label: "调整指令"
        },
        {
          value: 3,
          label: "移仓换月指令"
        }
      ],
      first_statusData: [
        {
          value: 0,
          label: "草拟"
        },
        {
          value: 1,
          label: "审核中"
        },
        {
          value: 4,
          label: "已通过"
        },
        {
          value: 2,
          label: "已驳回"
        },
        {
          value: 3,
          label: "已退审"
        },
        {
          value: 5,
          label: "已下达"
        },
        {
          value: 6,
          label: "已撤销"
        }
      ]
    };
  },
  created() {
    this.selectData();
    currency().then(res => {
      this.bz = res.result.results;
    });
  },
  methods: {
    //比较时间
    checkTime(stime) {
      var dt = new Date();
      var sdate = new Date(Date.parse(stime.replace(/-/g, "/"))); //当前指令结束时间
      let dat = this.formatDate_ss(dt);
      var edate = new Date(Date.parse(dat.replace(/-/g, "/")));
      var smonth = sdate.getMonth() + 1;
      var syear = sdate.getFullYear();
      var sday = sdate.getDate(); //当前指令结束时间

      var emonth = edate.getMonth() + 1;
      var eyear = edate.getFullYear();
      var eday = edate.getDate();
      if (syear > eyear) {
        return true;
      } else {
        if (smonth > emonth) {
          return true;
        } else {
          if (sday > eday) {
            return true;
          } else {
            return false;
          }
        }
      }
    },
    formatDate_ss(dt) {
      if (!dt) {
        dt = new Date();
      }
      var year = dt.getFullYear();
      var month = dt.getMonth() + 1;
      var date = dt.getDate();
      if (month < 10) {
        // 强制类型转换
        month = "0" + month;
      }
      if (date < 10) {
        // 强制类型转换
        date = "0" + date;
      }
      // 强制类型转换
      return year + "-" + month + "-" + date;
    },
    //刷新
    handleCleanCacheAndRefreshCurrent() {
      sessionStorage.setItem("activeName", 3);
      this.$nextTick();
      this.$router.replace("/refresh");
    },
    selectDataone() {
      this.form_first.page = 1;
      this.selectData();
    },
    // 查询
    selectData() {
      getFutures_command_info(this.form_first).then(res => {
        this.tableData_first = res.result.results;
        this.form_first.pageTotal = res.result.count;
      });
    },
    handlePaginationChange(val) {
      this.form_first.page = val;
      this.selectData();
    },
    handleCurrentChange_first(val) {
      this.currentRow_first = val;
    },
    // 期权指令修改
    handleFilter_editCkgl() {
      if (this.currentRow_first === null) {
        this.$message({
          message: "请先选择期权指令",
          type: "warning"
        });
      } else {
        if (
          this.currentRow_first.status == "草拟" ||
          this.currentRow_first.status == "已驳回"
        ) {
          this.$router.push(
            "/update_qiquan_orders?id=" + this.currentRow_first.command_id
          );
        } else {
          this.$message({
            message: "不可修改",
            type: "warning"
          });
        }
      }
    },
    delete_fn() {
      if (this.currentRow_first === null) {
        this.$message({
          message: "请先选择期权指令",
          type: "warning"
        });
      } else {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            if (
              this.currentRow_first.status == "草拟" ||
              this.currentRow_first.status == "已驳回"
            ) {
              // console.log(this.currentRow_first)
              var datas = {
                operation: "DEL",
                command_id: this.currentRow_first.command_id
              };
              futures_command_info(datas)
                .then(res => {
                  this.selectData();
                  this.$message({
                    type: "success",
                    message: res.result
                  });
                })
                .catch(err => {
                  this.$message({
                    message: err.msg,
                    type: "warning"
                  });
                });
            } else {
              this.$message({
                message: "只有草拟和已驳回可删除操作",
                type: "warning"
              });
            }
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      }
    },
    look_fn() {
      if (this.currentRow_first === null) {
        this.$message({
          message: "请先选择期权指令",
          type: "warning"
        });
      } else {
        var datas = {
          command_id: this.currentRow_first.command_id
        };
        getFutures_command_info(datas).then(res => {
          this.look_first_data_t = res.result.results;
        });
        getFutures_command_trade(datas).then(res => {
          this.look_first_data_b = res.result.results;
          this.look_first_data_b.forEach(item => {
            this.bz.forEach(bzitem => {
              if (item.currency == bzitem.symbol) {
                item.currency = bzitem.name;
              }
            });
          });
          this.dialogVisible_look = true;
        });
      }
    },
    order_fn_first() {
      if (this.currentRow_first === null) {
        this.$message({
          message: "请先选择期权指令",
          type: "warning"
        });
      } else {
        if (!this.checkTime(this.currentRow_first.end_date)) {
          this.$message({
            message: "指令时间已结束",
            type: "warning"
          });
          return;
        }
        if (this.currentRow_first.status == "已通过") {
          this.$confirm("是否下达指令?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(() => {
              var data = {
                operation: "REL",
                command_id: this.currentRow_first.command_id
              };
              futures_command_info(data)
                .then(res => {
                  this.selectData();
                  this.$message({
                    type: "success",
                    message: res.result
                  });
                })
                .catch(err => {
                  this.$message({
                    message: err.msg,
                    type: "warning"
                  });
                });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取下达指令"
              });
            });
        } else {
          this.$message({
            message: "只有状态为'已通过',才可以下达指令",
            type: "warning"
          });
        }
      }
    },
    order_fn_first_null() {
      if (this.currentRow_first === null) {
        this.$message({
          message: "请先选择期权指令",
          type: "warning"
        });
      } else {
        if (this.currentRow_first.status != "已下达") {
          this.$message({
            message: "只有状态为'已下达',才可以撤销指令",
            type: "warning"
          });
          return;
        }
        this.$confirm("是否撤销指令?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            var data = {
              operation: "CAN",
              command_id: this.currentRow_first.command_id
            };
            futures_command_info(data)
              .then(res => {
                this.selectData();
                this.$message({
                  type: "success",
                  message: res.result
                });
              })
              .catch(err => {
                this.$message.error(err.msg);
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消撤销指令"
            });
          });
      }
    },
    //提交审批
    submit_first_dialogVisible_fn() {
      if (this.currentRow_first === null) {
        this.$message({
          message: "请先选择期权指令",
          type: "warning"
        });
        return;
      }
      this.loading_first = true;
      let objToStr = JSON.stringify(this.currentRow_first);
      var data = {
        operation: "ADD",
        task: "ORDER",
        desc: objToStr,
        business_id: this.currentRow_first.command_id
      };
      work_order(data)
        .then(res => {
          this.loading_first = false;
          this.selectData();
          this.$message({
            type: "success",
            message: res.result
          });
        })
        .catch(err => {
          this.loading_first = false;
          this.$message.error(err.msg);
        });
    },
    tiaozhuan() {
      if (this.currentRow_first === null) {
        this.$message({
          message: "请先选择期权指令",
          type: "warning"
        });
      } else {
        if (this.currentRow_first.workOrderID == 0) {
          this.$message({
            message: "未进入审批历史",
            type: "warning"
          });
          return;
        }
        this.$router.push(
          "/examine_historyss?id=" + this.currentRow_first.workOrderID
        );
      }
    },
    // 提交审批
    //  设计图上要选择审批人 但是张龙说暂时不用选
    // handleFilter_qhzlTjsp() {
    //   console.log(1)
    //   if (this.currentRow_first === null) {
    //     this.$message({
    //       message: "请先选择期权指令",
    //       type: "warning"
    //     });
    //   } else {
    //     this.submit_first_dialogVisible = true;
    //     getUsers().then(res => {
    //       this.submit_user_data = res.result;
    //     });
    //   }
    // },
    handleFilter_qhzlTjsp_tui() {
      if (this.currentRow_first === null) {
        this.$message({
          message: "请先选择期权指令",
          type: "warning"
        });
      } else {
        if (this.currentRow_first.status != "审核中") {
          this.$message({
            message: "只有状态为审核中才能退审",
            type: "warning"
          });
          return;
        }
        var data = {
          operation: "MOD",
          id: this.currentRow_first.workOrderID,
          status: 3,
          opinion: ""
        };
        work_order(data)
          .then(res => {
            this.selectData();
            this.$message({
              type: "success",
              message: res.result
            });
          })
          .catch(err => {
            this.$message.error(err.msg);
          });
      }
    },
    // 1.期权指令
    // 新增和赋值 1为新增其他为复制
    handleFilter_addqhzl(index) {
      this.$store.commit("SET_SELECTIONSTRATEGY_VUE", "");
      if (index == 1) {
        this.$router.push("/add_qiquan_orders?status=" + "null" + "&type=" + 1);
      } else {
        if (this.currentRow_first === null) {
          this.$message({
            message: "请先选择指令自成交",
            type: "warning"
          });
        } else {
          this.$router.push(
            "/add_qiquan_orders?status=" + this.currentRow_first.command_id
          );
        }
      }
    }
  }
};
</script>
